import request from "../utils/request";
// 获取列表
export const postActivityList = (data)=>{
    return request({
        url:'/activity/list',
        method:'POST',
        data:data
    })
}

// 新增
export const addActivity = (data)=>{
    return request({
        url:'/activity/add',
        method:'POST',
        data:data
    })
}

// 修改
export const updateActivity = (data)=>{
    return request({
        url:'/activity/update',
        method:'POST',
        data:data
    })
}

// 删除
export const deleteActivity = (activityId)=>{
    return request({
        url:'/activity/delete',
        method:'DELETE',
        data:{
            activityId
        }
    })
}

// 导出
export const exportActivity = async (data)=>{
    try {
        const url = import.meta.env.VITE_APP_API_URL;
        const response = await fetch(`${url}/activity/export`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data),
            responseType: 'blob', // 重要：指定响应类型为二进制流
        });

        if (!response.ok) {
            throw new Error('导出失败');
        }

        const blob = await response.blob();
        // 创建下载链接
        const urls = window.URL.createObjectURL(blob);
        const link = document.createElement("a");
        link.href = urls;
        link.setAttribute("download", "活动列表.xlsx"); // 设置下载文件名
        document.body.appendChild(link);
        link.click();
        // 释放资源
        document.body.removeChild(link);
        window.URL.revokeObjectURL(urls);
    } catch (error) {
        console.error("导出失败", error);
        alert("导出失败，请稍后重试");
    }
}